package com.netrust.cmsx.core.persist;

import java.util.Comparator;

import com.netrust.cmsx.utils.relfect.Reflections;

public class TreeEntityComp4Jq<T extends TreeEntity<T>> implements Comparator<TreeEntity<T>>{
	public int compare(TreeEntity<T> a, TreeEntity<T> b) {
		String pids_a=(String)Reflections.getFieldValue(a, "pids");   
		String pids_b=(String)Reflections.getFieldValue(b, "pids");   
		if(pids_a.split(",").length<pids_b.split(",").length){
			return 1;
		}else if(pids_a.split(",").length<pids_b.split(",").length){
			return -1;
		}else{
			
			if(Integer.parseInt(a.getPid())==Integer.parseInt(b.getPid())){
				Integer sort_a=(Integer)Reflections.getFieldValue(a, "sort");   
				Integer sort_b=(Integer)Reflections.getFieldValue(b, "sort");   
				if(sort_a>sort_b){
					return 1;
				}else if(sort_a<sort_b){
					return -1;
				}
				return 0;
			}else if(Integer.parseInt(a.getPid())>Integer.parseInt(b.getPid())){
				return 1;
			}
			return -1;
		}
	}
}
